#!/bin/sh
mkdir -p /work
if
ls /dev/sda3 2>&1 >/dev/null
then
swapon /dev/sda3 && SWAP_dev_status=0 || SWAP_dev_status=2
else
SWAP_dev_status=1
fi
if
ls /dev/sda4 2>&1 >/dev/null
then
mount /dev/sda4 /work && DOCKER_mount_dev_status=0 || DOCKER_mount_dev_status=2
else
DOCKER_mount_dev_status=1
fi

SDA_total_bytes=$(echo 'p\n' | fdisk /dev/sda | grep '/dev/sda' | head -n 2 | grep -Eo '[0-9]* bytes' | grep -Eo '[0-9]*')
SDA_total_sectors=$(expr $SDA_total_bytes / 512)
SDA_last_sector=$(echo 'p\n' | fdisk /dev/sda | grep '/dev/sda' | tail -n 1 | awk '{print $3}')
SDA_total_available_sector=$(expr $SDA_total_sectors - $SDA_last_sector)

# 可用空间
SDA_total_space=$(expr $SDA_total_available \* 512 / 1024)

if
[ $SWAP_dev_status -eq 1 ]
then
echo -e 'n\np\n3\n\n+1024M\nw\n' | fdisk /dev/sda
partprobe
mkswap /dev/sda3
swapon /dev/sda3
elif
[ $SWAP_dev_status -eq 2 ]
then
mkswap /dev/sda3
swapon /dev/sda3
fi
if
[ $DOCKER_mount_dev_status -eq 1 ]
then
echo -e 'n\np\n4\n\n\nw\n' | fdisk /dev/sda
partprobe
mkfs.ext4 /dev/sda4
mount /dev/sda4 /work
elif
[ $DOCKER_mount_dev_status -eq 2 ]
then
mkfs.ext4 /dev/sda4
mount /dev/sda4 /work
fi

# 挂着内核空间
mkdir -p /dev/mqueue && mount -t mqueue none /dev/mqueue
umount -l /sys/fs/cgroup
mount -t tmpfs  -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup/
for i in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups)
do
mkdir -p /sys/fs/cgroup/${i}
mount -t cgroup -o ${i} cgroup /sys/fs/cgroup/${i}
done
logger "CGROUPS mounted already"
# 执行docker
# if
# grep -q "/dev/sda4 /tmp/lib/docker" /proc/mounts
# then
# start-stop-daemon -S -b -x /usr/bin/docker -- daemon --storage-driver=devicemapper --registry-mirror=https://docker.mirrors.ustc.edu.cn --graph=/work
# else
# mount /dev/sda4 /work && start-stop-daemon -S -b -x /usr/bin/docker -- daemon --storage-driver=devicemapper --registry-mirror=https://docker.mirrors.ustc.edu.cn --graph=/work
# fi
# docker_pid=$(pidof docker)
# if [ -n "$docker_pid" ]; then
# 	logger "Docker Running on PID: $docker_pid"
# fi
# sysctl -w net.bridge.bridge-nf-call-iptables=1
# sysctl -w net.bridge.bridge-nf-call-ip6tables=1

# 创建管理员用户
# mkdir -m /home
# useradd admin -g 0 -m
# 修改密码
# cat <<EOF | passwd admin
# admin
# admin
# EOF